package engine.scene
{
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.flag.ObjType;
	import engine.logic.McPool;
	import engine.logic.tips.ItemMaxTips;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.text.TextField;
	
	import newx.NxSprite;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.text.NxLabel;
	
	public class SceneUtil
	{
		
		public static function getName( playerObj : SceneObject ) : String
		{
			var name : String;
			
			if(null == playerObj )
			{
				return "";
			}
			if( playerObj.Type == ObjType.Player )
			{
				name	=	playerObj.GetProperties("Name");	
				if(name != ModelLocator.getInstance().scene.Role.getPlayerName())
				{
					if( playerObj.GetPropertiesEx("GuildName") == "")
					{
						name=	StaticIni.getSwfLangStrVar( "UIString_1000295", [ name ] );
					}
					else
					{
						if(playerObj.GetPropertiesEx("GuildName") == ModelLocator.getInstance().scene.Role.GetPropertiesEx("GuildName"))
						{
							name=	StaticIni.getSwfLangStrVar( "UIString_1000299", [ name ] );
						}
						else
						{
							name=	StaticIni.getSwfLangStrVar( "UIString_1000295", [ name ] );
						}
					}
				}
				else
				{
					name=	StaticIni.getSwfLangStrVar( "UIString_1000296", [ name ] );
				}
			}else
			{
				name	=	StaticIni.getSwfLangStrVar( getObjTypeNameColor(playerObj.Type), [ StaticIni.getSwfLangStr( playerObj.GetProperties( "ConfigID" ) ) ] );
			}
			return name;	
		}
		
		
		
		/**
		 *获取帮派名字 
		 * @param playerObj
		 * @return 
		 * 
		 */
		public static function getFamilyName( playerObj : SceneObject ) : String
		{
			var name : String;
			
			if(null == playerObj )
			{
				return "";
			}
			if( playerObj.Type == ObjType.Player )
			{
				name	=	playerObj.GetProperties("GuildName");	
			
				if( playerObj.GetPropertiesEx("GuildName") == "")
				{
					name=	"";//StaticIni.getSwfLangStrVar( "UIString_1000295", [ name ] );
				}
				else
				{
					if(playerObj.GetPropertiesEx("GuildName") == ModelLocator.getInstance().scene.Role.GetPropertiesEx("GuildName"))
					{
						name=	StaticIni.getSwfLangStrVar( "UIString_1000492", [ name ] );
					}
					else
					{
						name=	StaticIni.getSwfLangStrVar( "UIString_1000493", [ name ] );
					}
				}
				
			}else
			{
				name	= "";
			}
			return name;	
		}
		
		/**
		 * 设置名字 
		 * @param name
		 * 
		 */		
		public static function setName(posMc:MovieClip, pMc : MovieClip, tObj : SceneObject):void
		{
			var		name		:String			=	SceneUtil.getName(tObj);
			
			for(var i : int = 0; i < pMc.numChildren; i++)
			{
				if(pMc.getChildAt(i).name == "mentxt")
				{
					var tempMc:TextField	=	pMc["mentxt"] as TextField;
					if(tempMc != null)
					{
						tempMc.mouseEnabled	=	false;
						tempMc.htmlText		=	name;
						
					}
					return;
				}
			}
			
			var		text		:NxLabel		=	new NxLabel();
			var 	color		:String			=	StaticIni.getIniVar("Css_txt_NpcSceneName","objType"+tObj.Type);
			var 	desStr		:String			= 	StaticIni.getSwfLangStrVar("textcolor",
				["#" + color, name]);
			
			text.htmlStrByBorder	=	desStr;
			text.scaleX	=	1/posMc.scaleX;
			text.scaleY	=	1/posMc.scaleY;
			text.y 		=	-pMc.height-30;;
			
			posMc.addChild(text);
			
			if (posMc.rotation > 0)
			{
				text.x		=	text.width*0.5 * text.scaleX;
				text.scaleX	*=	-1;
			}
			else
			{
				text.x		=	-text.width*0.5 * text.scaleX;
				text.scaleX	*=	1;
			}
		}
		public static function addNpcTaskIcon(posMc:MovieClip, pMc : MovieClip, tObj : SceneObject):void
		{
			return;
			var type:uint	=	tObj.Type;
			
			if(ObjType.NPC	!=	type) return;
			
			var icon : MovieClip	=	McPool.borrowRes("PkIcon");
			icon.scaleX	=	1/posMc.scaleX;
			icon.scaleY	=	1/posMc.scaleY;
			var tempY:Number	=	0;
			
			if (posMc.rotation > 0)
			{
				icon.x		=	icon.width*0.5 * icon.scaleX;
				icon.scaleX	*=	-1;
			}
			else
			{
				icon.x		=	-icon.width*0.5 * icon.scaleX;
				icon.scaleX	*=	1;
			}
			for(var i:int = 0; i < posMc.numChildren; i++)
			{
				var checkMc:DisplayObject	=	posMc.getChildAt(i) as DisplayObject;
				if(checkMc.y < tempY)
				{
					tempY =	checkMc.y;
				}
			}
			tempY	=	Math.min(tempY - icon.height,-200)
			icon.y 		=	tempY
			icon.name	=	"PkIcon"
			posMc.addChild(icon);
		}
		/**
		 *	加入npc图标 
		 * @param posMc
		 * 
		 */
		public static function addNpcStatusMc(posMc : MovieClip, clip : NxClip):void
		{
			return;
			for(var i:int = 0; i < posMc.numChildren; i++)
			{
				var checkMc:DisplayObject	=	posMc.getChildAt(i) as DisplayObject;
				
				if( checkMc is NxClip )
				{
					posMc.removeChild( checkMc );
					McPool.returnRes( checkMc.name, checkMc );
					break;
				}
			}
			
//			clip.scaleX	=	clip.scaleY	=	1/posMc.scaleX;
			
			posMc.addChild(clip);
		}
		
		/**
		 * 设置状态图标
		 * @param name
		 * 
		 */		
		public static function setStatusIcon(posMc:MovieClip, objId:String):void
		{
			return;
			var npcArray : Array	=	PlayerLogic.getInstance().getPlayerObj(ObjType.FunctionNpc, objId);
			
			if(npcArray == null)return;
			
			var url : String  = BaseUI.getIconByStatus(npcArray[1]);
			
			var img : NxClip = McPool.borrowRes( url );
			
			if( null == img )
			{
				img = new NxClip();
				
				if(img.content == null)
				{
					img.addLoadedEvent(function(val:DisplayObject):void
					{
						setIconPoint(posMc, img, val);
					},NxClip.NXCLIP_COMPLETE);
				}
				img.source	= BaseUI.getIconByStatus(npcArray[1]);
				img.name = url;
			}
			
			if( img.content != null )
			{
				setIconPoint(posMc, img, img.content);
			}
			
			SceneUtil.addNpcStatusMc(posMc, img);
		}
		/**
		 * 设置主角图标 
		 * @param posMc
		 */	
		public static function setSelfIcon(posMc:MovieClip):void
		{
			return;
			var selfIconTop:MovieClip	= McPool.borrowRes("RoleIcon1") as MovieClip;
			var selfIconBotton:MovieClip	= McPool.borrowRes("RoleIcon0") as MovieClip;
			var tempY:Number	=	0;
			selfIconBotton.scaleX	=	selfIconTop.scaleX	=	1/posMc.scaleX;
			selfIconBotton.scaleY	=	selfIconTop.scaleY	=	1/posMc.scaleY;
			posMc.addChild(selfIconTop);
			selfIconBotton.y	=	 -10;
			selfIconTop.name = "RoleIcon1";
			selfIconBotton.name = "RoleIcon0";
			(selfIconBotton.getChildAt(0) as MovieClip).play();
			(selfIconTop.getChildAt(0) as MovieClip).play();
			posMc.addChildAt(selfIconBotton,0);
//			posMc.mouseChildren = posMc.mouseEnabled = false;
		}
		private static function setIconPoint(posMc : MovieClip, img : NxSprite, content : DisplayObject):void
		{
			return;
			var tempY:Number	=	0;
			img.scaleX	=	posMc.scaleX;
			img.x		=	content.width * img.scaleX * .5;
			if (posMc.rotation > 0)
			{
				img.scaleX	*=	-1;
			}
			else
			{
				img.x		=	img.x * -1;
			}
			for(var i:int = 0; i < posMc.numChildren; i++)
			{
				var checkMc:DisplayObject	=	posMc.getChildAt(i) as DisplayObject;
				if(checkMc.y < tempY && !(checkMc is NxClip))
				{
					tempY =	checkMc.y;
				}
			}
			tempY	=	Math.min(tempY - img.height,-200)
			img.y	=	tempY;
		}
		
		public static function getItemName( configId : String ):String
		{
			var itemObj : Object = StaticIni.getIniObj( configId );
			return link_color( StaticIni.getSwfLangStr( configId ), itemObj.Color );
		}
		
		private static function link_color(str:String,color:int):String
		{
			if(color < 1 || color >8)
			{
				color	=	1;
			}
			return ("<font color ='"+ ItemMaxTips.COLOR_TABLE[color]+str+"</font>");
		}
		/**
		 * 根据对象类型获取特定的文本颜色 
		 * @param val
		 * @return 
		 * 
		 */		
		public static function getObjTypeNameColor(val:uint):String
		{
			var colorStr:String	=	"UIString_1000301";
			switch(val)
			{
				case ObjType.Player:
					colorStr	=	"UIString_1000295";
					break;
				case ObjType.FunctionNPC:
				case ObjType.TaskNPC:
				case ObjType.DepotNPC:
				case ObjType.Door:
					colorStr	=	"UIString_1000301";
					break;
				case ObjType.FightNPC:
				case ObjType.NPC:
					colorStr	=	"UIString_1000300";
					break;
			}
			return colorStr;
		}
	}
}